package com.wealth.system.web.controller;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.wealth.system.entity.Corporation;
import com.wealth.system.entity.User;
import com.wealth.system.service.ICorpService;
import com.wealth.system.service.IUserService;
import com.wealth.system.service.ShiroUser;

import iaas.enums.StatusEnum;
import iaas.utils.RedirectMessage;
import iaas.utils.ResultObject;
import iaas.web.JqGridRequest;
import iaas.web.JqGridResponse;
import iaas.web.JqGridUtil;

/**
 * 企业管理
 * @author chan
 *
 */
@Controller
@RequestMapping(value="/manage/system/corp")
public class CorpController extends BaseController{

	Logger logger = Logger.getLogger(CorpController.class);
	
	@Autowired
	private ICorpService corpService;
	@Autowired
	private IUserService userService;
	
	/**
	 * 企业列表
	 */
	@RequestMapping(value="/list",method = RequestMethod.GET)
	public String toList(Model model){
		return "system/corp/list";
	}
	
	
	/**
	 * 查询数据
	 */
	@RequestMapping(value="/list",method = RequestMethod.POST)
	@ResponseBody
	public JqGridResponse getJson(Model model,JqGridRequest request) {
		try{
			
			Long corpid = ShiroUser.getInstance().getUser().getCorp().getId();
			
			Page<Corporation> page = corpService.get(corpid, request,Corporation.class);
			
			return JqGridUtil.getJqGrirdResponse(page);
		}catch(Exception e){
			logger.error("企业列表错误！", e);
			return null;
		}
	}
	
	/**
	 * 开户页面
	 */
	@RequestMapping(value="/create",method = RequestMethod.GET)
	public String createForm(){
		return "system/corp/create";
	}
	
	/**
	 * 开户
	 */
	@RequestMapping(value="/create",method = RequestMethod.POST)
	public String create(Model model,Corporation corp,User user,String corpname,String domainPC,String domainWAP,RedirectAttributes redirectAttributes){
		try {
			//查询企业账户是否存在如果存在则提示
			boolean con = false;
			if(con){
				redirectAttributes.addFlashAttribute("message", RedirectMessage.setShowTipError("账号已经存在，不能完成开户"));
				return "redirect:/manage/system/corp/create";
			}
			//保存企业
			corp.setName(corpname);
			corp.setCreator(ShiroUser.getInstance().getUser().getUsername());
			corp.setCreatetime(new Date());
			corp.setStatus(StatusEnum.NORMAL.getValue());
			corpService.openAccount(corp, user);
			
			redirectAttributes.addFlashAttribute("message", RedirectMessage.setShowTipSuccess("开户成功，请牢记您的账户"+user.getUsername()));
			return "redirect:/manage/system/corp/list";
		} catch (Exception e) {
			logger.error("开户错误", e);
			return "system/500";
		}
	}
	
	/**
	 * 编辑企业账户页面
	 */
	@RequestMapping(value="/update/{id}",method = RequestMethod.GET)
	public String setUpdateForm(Model model,@PathVariable("id") Long id){
		try{
			//企业
			Corporation corp = corpService.get(id);
			//查询该企业管理员
			User user =null;
			if(id!=1L){
				user = userService.findCorpAmin(id);
			}else{
				user = userService.get(1L);
			}
			
			
			model.addAttribute("corp", corp);
			model.addAttribute("user", user);
			
			return "system/corp/update";
		}catch(Exception e){
			logger.error("编辑企业账户页面错误,企业id="+id, e);
			return "system/500";
		}
	}
	
	/**
	 * 修改企业账户信息
	 */
	@RequestMapping(value="/update/{id}",method = RequestMethod.POST)
	public String update(@PathVariable("id") Long id,Corporation corp,User user,
			String corpname,String domainPC,String domainWAP,Long userid,RedirectAttributes redirectAttributes){
		try{
			corp.setId(id);
			corp.setName(corpname);
			user.setId(userid);
			corpService.updateAccount(corp,user);
			

			redirectAttributes.addFlashAttribute("message", RedirectMessage.setShowTipSuccess("编辑企业成功"));
			return "redirect:/manage/system/corp/list";
		}catch(Exception e){
			logger.error("编辑企业账户错误,企业id="+id, e);
			return "system/500";
		}
	}
	
	/**
	 * 删除企业信息（逻辑删除）
	 */
	@RequestMapping(value="/delete/{id}",method = RequestMethod.GET)
	public String delete(@PathVariable Long id,RedirectAttributes redirectAttributes){
		try{
			if(id==1){
				redirectAttributes.addFlashAttribute("message", RedirectMessage.setShowTipError("基础数据不能删除"));
				return "redirect:/manage/system/corp/list";
			}
			corpService.deleteLogical(id);	
			redirectAttributes.addFlashAttribute("message", RedirectMessage.setShowTipWarning("删除企业成功"));
			return "redirect:/manage/system/corp/list";
		}catch(Exception e){
			logger.error("删除企业账户错误,企业id="+id,e);
			return "system/500";
		}
	}
	
	
	/**
	 * 停用
	 */
	@RequestMapping(value="/disable/{id}",method = RequestMethod.GET)
	public String disable(@PathVariable Long id,RedirectAttributes redirectAttributes){
		try{
			corpService.updateStatus(id, StatusEnum.STOP.getValue());
			redirectAttributes.addFlashAttribute("message", RedirectMessage.setShowTipWarning("停用成功"));
			return "redirect:/manage/system/corp/list";
		}catch(Exception e){
			logger.error("停用错误,id="+id,e);
			return "system/500";
		}
	}
	
	/**
	 * 启用
	 */
	@RequestMapping(value="/enable/{id}",method = RequestMethod.GET)
	public String enable(@PathVariable Long id,RedirectAttributes redirectAttributes){
		try{
			corpService.updateStatus(id, StatusEnum.NORMAL.getValue());
			redirectAttributes.addFlashAttribute("message", RedirectMessage.setShowTipWarning("启用成功"));
			return "redirect:/manage/system/corp/list";
		}catch(Exception e){
			logger.error("启用错误,id="+id,e);
			return "system/500";
		}
	}
	
	
	/**
	 * 企业名称是否唯一
	 * 2015-07-15 by chan
	 * ----------- 参数说明 ----------
	 * corpname 用户名
	 * 
	 * ----------- 返回说明 --------
	 * true 表示企业名称唯一
	 * false 表示存在相同的企业名称
	 */
	@RequestMapping(value="/unique/corpname",method = RequestMethod.POST)
	@ResponseBody
	public boolean unique(String corpname,Model model){
		try{
			return corpService.unique(corpname);
		}catch(Exception e){
			logger.error("检查企业名称是否存在错误！！", e);
			return false;
		}
	}
	
	
	/**
	 * Ajax获取企业信息
	 */
	@RequestMapping(value="/getCorp",method = RequestMethod.POST)
	@ResponseBody
	public ResultObject getCorp(Model model){
		try{
			List<Corporation> corporations=corpService.findByStatus(StatusEnum.NORMAL.getValue());
			
			return new ResultObject("200", "查询成功", corporations);
		}catch(Exception e){
			logger.error("检查企业名称是否存在错误！！", e);
		}
		return null;
	}
	
}
